/**
 *@Author: QWP
 *@Description: 路由的主入口
 *@Params:
 *@Date 2023-06-18 11:29
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { useLoginStore } from "@/store";

import sys_router from "@/router/system";
import oaRouter from "@/router/oa";
import bmpRouter from "./bmp";
import { messageC } from "../util/common.js";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    component: () => import("@/views/login/Login.vue"),
  },
  {
    path: "/TransitIndex",
    name: "TransitIndex",
    component: () => import("@/views/index/TransitIndex.vue"),
    meta: { keepalive: true },
    redirect: "/TransitIndex/Index",
    children: [
      {
        path: "Index",
        name: "Index",
        meta: { keepalive: true },
        component: () => import("@/views/index/index/Index.vue"),
      },
      ...sys_router,
      ...oaRouter,
      ...bmpRouter,
    ],
  },
  {
    // 附件预览
    path: "/FileEcho",
    name: "FileEcho",
    component: () => import("@/views/common/UploadEcho.vue"),
  },
  {
    // 资源下载
    path: "/Resource",
    name: "Resource",
    component: () => import("@/views/common/Resource.vue"),
  },
  {
    // APP下载
    path: "/AppDownload",
    name: "AppDownload",
    component: () => import("@/views/common/DownloadApp.vue"),
  },
  {
    // APPoffice在线编辑
    path: "/OfficeDocMobile",
    name: "OfficeDocMobile",
    component: () => import("@/views/common/OfficeDocMobile.vue"),
  },
  {
    path: "/404",
    component: () => import("@/views/index/404.vue"),
  },
  // {
  //   path: "/Test",
  //   component: () => import("@/views/test/Test.vue"),
  // },
  {
    path: "/:pathMatch(.*)*",
    redirect: "/404",
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

/**
 *@Author: QWP
 *@Description: 全局前置路由守卫
 *@Date 2023-04-07 09:09
 */
router.beforeEach((to, from, next) => {
  const loginStore = useLoginStore();
  if (["/AppDownload", "/OfficeDocMobile", "/Resource"].includes(to.path)) {
    next();
  } else if (loginStore.token && to.path == "/" && to.path != from.path) {
    next(from.path);
  } else if (!loginStore.token && to.path != "/") {
    messageC("请先登录系统!");
    next("/");
  } else {
    next();
  }
});

export default router;
